var vm_deptdetail = new Vue({
    el: "#deptdetail",
    data: {
        oldparentid: $("#olddeptid").val(),		// 单位ID
        parentid: $("#deptid").val(),		// 单位ID
        roleid: $("#roleid").val(),		// 单位ID

        danweichaoguanroleid:100,
        zjzxroleid:102,

        deptlist: [],// 单位列表custom
        deptid: '',//单位id
        dept: {},//单位

        //查询用字段
        jigoumingcheng: '',
        jigoudaima: '',
        jigouleixing: '',
        diqu: '',
        //

        diqusearch:'',
        editflag: '',//1添加 2修改


        userlist: [],//单位用户列表
        islistshow: true,//人员列表显示
        isuseraddshow: false,//用户添加显示
        isusereditshow: false,//用户修改显示
        userdeptid: '',//用户部门id
        user : {},//修改用户

    },
    created: function () {
        var _this = this;
        _this.bindDeptList();
        $.ajaxSettings.async = false;

    },


    methods: {

        // 根据单位ID查询下级单位
        bindDeptList: function () {
            var _this = this;

            var dept_layer = layer.open({type: 3});
            $.post('/system/deptdetail/customlist', {
                parentid: _this.oldparentid,
                jigoumingcheng: _this.jigoumingcheng,
                jigoudaima: _this.jigoudaima,
                jigouleixing: _this.jigouleixing,
                beizhu1: _this.diqu,
                rdm: Math.random()
            }, function (ppData) {
                layer.close(dept_layer);

                if (ppData != null) {
                    var data = ppData;
                    _this.deptlist = data;
                }
            }, "json");
        },

        bindDept: function () {
            var _this = this;

            var dept_layer = layer.open({type: 3});
            $.post('/system/deptdetail/findone', {
                deptid: _this.deptid,
                rdm: Math.random()
            }, function (ppData) {
                layer.close(dept_layer);

                if (ppData != null) {

                    if (ppData.result == "1") {
                        var data = ppData.resultContent;
                        _this.dept = data;
                        _this.dept.deptid = _this.deptid;
                    } else {
                        layer.alert(ppData.message);
                    }
                }
            }, "json");
        },

        //添加
        searchdiqu: function () {
            var _this = this;
            $.post('/system/deptdetail/searchdiqu', {
                diqu: _this.diqusearch,
                rdm: Math.random()
            }, function (ppData) {
                if (ppData != null) {

                    if (ppData.result == "1") {
                        $("#beizhu1").html("");

                        var diqulist = ppData.resultContent;
                        for (var i=0;i<diqulist.length;i++){
                            $("#beizhu1").append("<option value='"+ diqulist[i].dict_label +"' >"+ diqulist[i].dict_label +"</option>");
                        }

                    } else {
                        layer.alert(ppData.message);
                    }
                }
            }, "json");
        },

        toAdd: function () {
            $("#myModalLabel").html("添加下级单位");
            $("#modal-dialog").attr("style", "width:1200px;");
            $("#deptModal").modal();

            this.dept = {};
            this.editflag = 1;
        },

        //添加
        add: function () {
            var _this = this;
            if (_this.checkInputData()) {
                var dept_layer = layer.open({type: 3});
                $.post('/system/deptdetail/save', {
                    parentid: _this.oldparentid,
                    jigoudaima: _this.dept.jigoudaima,
                    jigoumingcheng: _this.dept.jigoumingcheng,
                    jigoujibie: _this.dept.jigoujibie,
                    jigouleixing: _this.dept.jigouleixing,
                    beizhu1: _this.dept.beizhu1,
                    dizhi: _this.dept.dizhi,
                    youbian: _this.dept.youbian,
                    chuanzhenghaoma: _this.dept.chuanzhenghaoma,
                    dianziyoujian: _this.dept.dianziyoujian,
                    jingbanren: _this.dept.jingbanren,
                    jingbanrenbangongdianhua: _this.dept.jingbanrenbangongdianhua,
                    jingbanrenshoujihaoma: _this.dept.jingbanrenshoujihaoma,
                    fuzeren: _this.dept.fuzeren,
                    fuzerenbangongdianhua: _this.dept.fuzerenbangongdianhua,
                    fuzerenshoujihaoma: _this.dept.fuzerenshoujihaoma,
                    caiwulianxiren: _this.dept.caiwulianxiren,
                    caiwubangongdianhua: _this.dept.caiwubangongdianhua,
                    caiwushoujihaoma: _this.dept.caiwushoujihaoma,
                    kaihuhang: _this.dept.kaihuhang,
                    kaihumingcheng: _this.dept.kaihumingcheng,
                    yinhangzhanghao: _this.dept.yinhangzhanghao,
                    yinhangdizhi: _this.dept.yinhangdizhi,
                    lianhanghao: _this.dept.lianhanghao,
                    beizhu3: _this.dept.beizhu3,
                    rdm: Math.random()
                }, function (ppData) {
                    layer.close(dept_layer);
                    if (ppData != null) {

                        if (ppData.result == "1") {
                            layer.open({
                                time: 1000,
                                btn: [],
                                content: "添加成功!",
                            });

                            _this.bindDeptList();
                            $("#deptModal").modal('hide');

                        } else {
                            layer.alert(ppData.message);
                        }
                    }
                }, "json");
            }
        },

        toModify: function (ppDeptId) {

            $("#myModalLabel").html("修改单位信息");
            $("#modal-dialog").attr("style", "width:1200px;");
            $("#deptModal").modal();

            this.deptid = ppDeptId;

            this.dept = {};
            this.editflag = 2;
            // 绑定专家
            this.bindDept();
        },

        // 修改信息
        modify: function () {
            var _this = this;
            if (_this.checkInputData()) {
                var dept_layer = layer.open({type: 3});
                $.post('/system/deptdetail/save', {
                    id: _this.deptid,
                    jigoudaima: _this.dept.jigoudaima,
                    jigoumingcheng: _this.dept.jigoumingcheng,
                    jigoujibie: _this.dept.jigoujibie,
                    jigouleixing: _this.dept.jigouleixing,
                    beizhu1: _this.dept.beizhu1,
                    dizhi: _this.dept.dizhi,
                    youbian: _this.dept.youbian,
                    chuanzhenghaoma: _this.dept.chuanzhenghaoma,
                    dianziyoujian: _this.dept.dianziyoujian,
                    jingbanren: _this.dept.jingbanren,
                    jingbanrenbangongdianhua: _this.dept.jingbanrenbangongdianhua,
                    jingbanrenshoujihaoma: _this.dept.jingbanrenshoujihaoma,
                    fuzeren: _this.dept.fuzeren,
                    fuzerenbangongdianhua: _this.dept.fuzerenbangongdianhua,
                    fuzerenshoujihaoma: _this.dept.fuzerenshoujihaoma,
                    caiwulianxiren: _this.dept.caiwulianxiren,
                    caiwubangongdianhua: _this.dept.caiwubangongdianhua,
                    caiwushoujihaoma: _this.dept.caiwushoujihaoma,
                    kaihuhang: _this.dept.kaihuhang,
                    kaihumingcheng: _this.dept.kaihumingcheng,
                    yinhangzhanghao: _this.dept.yinhangzhanghao,
                    yinhangdizhi: _this.dept.yinhangdizhi,
                    lianhanghao: _this.dept.lianhanghao,
                    beizhu3: _this.dept.beizhu3,
                    rdm: Math.random()
                }, function (ppData) {
                    layer.close(dept_layer);
                    if (ppData != null) {

                        if (ppData.result == "1") {
                            layer.open({
                                time: 1000,
                                btn: [],
                                content: "修改成功!",
                            });

                            _this.bindDeptList();
                            $("#deptModal").modal('hide');

                        } else {
                            layer.alert(ppData.message);
                        }
                    }
                }, "json");
            }
        },

        //检查项目数据
        checkInputData: function () {
            if (!$.trim(this.dept.jigoumingcheng)) {
                layer.alert("请填写单位名称！");
                return false;
            }

            return true;
        },

        zhuxiao:function(ppDeptId){
            var _this = this;
            if (confirm("确定要注销该单位吗？")) {
                var biandong_layer = layer.open({type:3});
                $.post('/system/deptdetail/zhuxiao', {
                    deptid: ppDeptId,
                    rdm: Math.random()
                }, function (ppData) {
                    layer.close(biandong_layer);

                    if (ppData != null) {

                        if (ppData.result == "1") {

                            if (ppData.resultContent.code=='b'){
                                layer.open({
                                    time: 1000,
                                    btn: [],
                                    content: ppData.resultContent.msg,
                                });
                            }
                            if(ppData.resultContent.code=='a'){
                                if (confirm(ppData.resultContent.msg+"，是否进入专家转移功能？")){
                                    _this.toZhuanjiazhuanyi(ppDeptId);
                                }
                            }
                            if (ppData.resultContent.code=='c'){
                                layer.open({
                                    time: 1000,
                                    btn: [],
                                    content: ppData.resultContent.msg,
                                });
                            }

                            _this.bindDeptList();
                        } else {
                            layer.alert(ppData.message);
                        }
                    }
                }, "json");
            }
        },

        refresh: function () {
            var _this = this;

            _this.jigoumingcheng=""
            _this.diqu=""
            _this.jigouleixing=""
            _this.bindDeptList();
        },

        showUser: function (ppDeptId) {
            var _this = this
            _this.bindUserList(ppDeptId)
            $("#userModal").modal();
            _this.userdeptid = ppDeptId
            $('#userModal').on('hidden.bs.modal', function () {
                _this.islistshow = true
                _this.isuseraddshow = false
                _this.isusereditshow = false
            })

        },

        //绑定用户列表
        bindUserList: function (ppDeptId) {
            var _this = this

            $.post('/system/user/selectuserlistbydeptid', {
                deptid: ppDeptId,
            }, function (ppData) {
                if (ppData != null) {
                    var data = ppData;
                    _this.userlist = data
                } else {
                    layer.alert(ppData.message);
                }

            }, "json");
        },

        toZhuanjiazhuanyi:function(ppDeptId){
            location.href="/system/deptzhuanjia/zhuanjiazhuanyi?deptid="+ppDeptId;
        },

        //新增
        userAdd: function () {
            var _this = this
            _this.islistshow = false
            _this.isuseraddshow = true
        },

        closeUserAdd: function () {
            var _this = this
            _this.bindUserList(_this.userdeptid)
            _this.islistshow = true
            _this.isuseraddshow = false
        },
        //提交新增用户
        submitHandler() {
            var _this = this
            var prefix =  "/system/user";
            $("#form-user-add").validate({
                onkeyup: false,
                rules:{
                    userName:{
                        required:true
                    },
                    loginName:{
                        minlength: 2,
                        maxlength: 20,
                        remote: {
                            url: prefix + "/checkLoginNameUnique",
                            type: "post",
                            dataType: "json",
                            async:false,
                            data: {
                                "loginName": function() {
                                    return $.common.trim($("#loginName").val());
                                }
                            },
                            dataFilter: function(data, type) {
                                return $.validate.unique(data);
                            }
                        }
                    },
                    password:{
                        minlength: 5,
                        maxlength: 20
                    },
                    email:{
                        email:true,
                        remote: {
                            url: prefix + "/checkEmailUnique",
                            type: "post",
                            dataType: "json",
                            async:false,
                            data: {
                                "email": function () {
                                    return $.common.trim($("#email").val());
                                }
                            },
                            dataFilter: function (data, type) {
                                return $.validate.unique(data);
                            }
                        }
                    },
                    phonenumber:{
                        isPhone:true,
                        remote: {
                            url: prefix + "/checkPhoneUnique",
                            type: "post",
                            dataType: "json",
                            async:false,
                            data: {
                                "phonenumber": function () {
                                    return $.common.trim($("#phonenumber").val());
                                }
                            },
                            dataFilter: function (data, type) {
                                return $.validate.unique(data);
                            }
                        }
                    },
                },
                messages: {
                    "loginName": {
                        remote: "用户已经存在"
                    },
                    "email": {
                        remote: "Email已经存在"
                    },
                    "phonenumber":{
                        remote: "手机号码已经存在"
                    }
                },
                focusCleanup: true
            });

            var password = $("#password").val();
            console.log($.validate.form("form-user-add"))//删除会出现bug：false也会进if
            if ($.validate.form("form-user-add") ) {
                var data = $("#form-user-add").serializeArray();
                var roleIds = $("#role").val()
                data.push({"name": "roleIds", "value": roleIds});
                data.push({"name": "olddeptId", "value": _this.userdeptid});
                $.operate.post(prefix + "/modaluseradd", data);
                _this.bindUserList(_this.userdeptid)
                setTimeout(function(){
                        _this.islistshow = true
                        _this.isuseraddshow = false
                    }, 10,)//删掉后模态框大概率成功后关闭
            }
        },
        //修改
        userModify(user){
            var _this = this
            _this.user = user
            _this.islistshow=false
            _this.isusereditshow=true
        },
        resetPwd:function (user) {
            var url = 'system/user/yonghuguanli/resetPwd/' + user.user_id;
            $.modal.open("重置密码", url, '800', '300');
        },
        //提交用户修改
        submitUserEdit(){
            var _this = this
            var prefix = "/system/user";

            $("#form-user-edit").validate({
                onkeyup: false,
                rules:{
                    userName:{
                        required:true
                    },
                    email:{
                        email:true,
                        remote: {
                            url: prefix + "/checkEmailUnique",
                            type: "post",
                            dataType: "json",
                            data: {
                                "userId": function() {
                                    return $("#userId").val();
                                },
                                "email": function() {
                                    return $.common.trim($("#email").val());
                                }
                            },
                            dataFilter: function (data, type) {
                                return $.validate.unique(data);
                            }
                        }
                    },
                    phonenumber:{
                        isPhone:true,
                        remote: {
                            url: prefix + "/checkPhoneUnique",
                            type: "post",
                            dataType: "json",
                            data: {
                                "userId": function() {
                                    return $("#userId").val();
                                },
                                "phonenumber": function() {
                                    return $.common.trim($("#phonenumber").val());
                                }
                            },
                            dataFilter: function (data, type) {
                                return $.validate.unique(data);
                            }
                        }
                    },
                },
                messages: {
                    "email": {
                        remote: "Email已经存在"
                    },
                    "phonenumber":{
                        remote: "手机号码已经存在"
                    }
                },
                focusCleanup: true
            });
            console.log($.validate.form("form-user-edit"))//删除会出现bug
            if ($.validate.form("form-user-edit")) {
                var data = $("#form-user-edit").serializeArray();
                $.operate.post(prefix + "/modaluseredit", data);
                _this.bindUserList(_this.userdeptid)
                setTimeout(function(){
                    _this.islistshow = true
                    _this.isusereditshow = false
                }, 10,)//删掉后模态框大概率成功后关闭

            }
        },
        closeUserEdit(){
            var _this = this
            _this.bindUserList(_this.userdeptid)
            _this.islistshow = true
            _this.isusereditshow = false

        },
        userRemove(userId){
            var _this = this
            $.modal.confirm("确认删除", function() {
                $.operate.get("/system/user/modaluserremove?userId="+userId)
                _this.bindUserList(_this.userdeptid)
            });
        },

        toXiaji:function (deptitem) {
            $.modal.openTab("下级单位","/system/deptdetail/modaldept?deptid="+deptitem.id+"&jigoujibie="+deptitem.jigoujibie);
        },
        toChakan: function (deptid) {
            $("#myModalLabel1").html("单位信息");
            $("#modal-dialog1").attr("style", "width:1200px;");
            $("#deptModal1").modal();
            this.deptid=deptid
            this.dept = {};
            this.bindDept();
        },

        toLianxifangshi:function (deptid) {
            var _this = this;
            this.deptid=deptid
            this.dept = {};
            this.bindDept();
            $("#lianxifangshiModal").modal();
        },

    }


})
